package org.sanm.domain.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sanm.common.Result;
import org.sanm.domain.company.Company;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
@Tag(name = "商品管理", description = "商品管理")
@RequiredArgsConstructor
public class GoodsController {

    private final GoodsService goodsService;

    @PostMapping("add")
    @Operation(summary = "添加商品", description = "添加商品")
    public Result<Goods> add(@RequestBody Goods goods){
//        goods.setCreateTime(LocalDateTime.now());
//        goods.setUpdateTime(LocalDateTime.now());
        Db.save(goods);
        return Result.success(goods);
    }

    @PutMapping("update")
    @Operation(summary = "修改商品", description = "修改商品")
    public Result update(@RequestBody Goods goods){
        Db.updateById(goods);
        return Result.success("修改成功");
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除商品", description = "删除商品")
    public Result delete(@PathVariable("id") Integer id){
        Db.removeById(id, Goods.class);
        return Result.success("删除成功");
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id商品", description = "根据id商品")
    public Result<Goods> getDetail(@PathVariable("id") Integer id){

        return Result.success(goodsService.getDetail(id));
    }

    @GetMapping("ids")
    @Operation(summary = "根据多个id查询商品", description = "根据多个id查询商品")
    public Result<List<Goods>> getByIds(@RequestParam("ids") List<Integer> ids){

        return Result.success(goodsService.getByIds(ids));
    }

    @GetMapping("list")
    @Operation(summary = "分页查询商品", description = "分页查询商品")
    public Result<Page<Goods>> page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "companyId", required = false) Integer companyId,
            @RequestParam(value = "categoryId", required = false) Integer categoryId
    ){

        Page<Goods> page = goodsService.page(pageNum, pageSize, name, companyId, categoryId);
        return Result.success(page);
    }
}
